package ar.com.spsolutions.splilabrary.view.pages.book.loan;

import java.util.List;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.springframework.transaction.annotation.Transactional;

import ar.com.spsolutions.splibrary.book.Book;
import ar.com.spsolutions.splibrary.book.LoanBook;
import ar.com.spsolutions.splibrary.services.LibraryServiceFacade;
import ar.com.spsolutions.splibrary.user.User;
import ar.com.spsolutions.splilabrary.view.application.WebSession;
import ar.com.spsolutions.splilabrary.view.components.modalwindow.SelectModalWindow;
import ar.com.spsolutions.splilabrary.view.pages.book.comment.NewComment;
import ar.com.spsolutions.splilabrary.view.version2.components.MainContainer;

import com.google.code.jqwicket.ui.prettypopin.PrettyPopinBehavior;
import com.google.code.jqwicket.ui.prettypopin.PrettyPopinOptions;

public class LoanEditComponent extends MainContainer {

    private static final long serialVersionUID = -259962111017896326L;

    private static Book book;

    private static LibraryServiceFacade libraryServiceFacadeLEC;

    private SelectModalWindow feedbackModalWindow;

    private void getCommentLink(final ListItem<LoanBook> item, final LoanBook loan) {
        final Link<Void> linkComment = new Link<Void>("linkComment") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                LoanEditComponent.setBook(LoanEditComponent.this.getLibraryServiceFacade().getEntityService()
                        .getBookService().findByName(LoanEditComponent.this.getBookTitle(loan)));
                LoanEditComponent.setLibraryServiceFacadeLEC(LoanEditComponent.this.getLibraryServiceFacade());
                this.setResponsePage(NewComment.class);
            }
        };
        linkComment.add(new PrettyPopinBehavior(new PrettyPopinOptions().width(500).height(400).followScroll(false)));
        item.add(linkComment);
    }

    @Override
    protected void addComponents() {

        this.add(new ListView<LoanBook>("loanRow", getLoans()) {

            private static final long serialVersionUID = 6844172313502472316L;

            @Override
            @Transactional
            protected void populateItem(final ListItem<LoanBook> item) {
                feedbackModalWindow = new SelectModalWindow("feedbackLoan");
                item.add(feedbackModalWindow);
                final LoanBook loan = item.getModelObject();
                item.setVisible(!loan.isRefun());
                item.add(new Label("bookName", LoanEditComponent.this.getBookTitle(loan)));
                item.add(new Label("refunDate", LoanEditComponent.this.getRefunDate(loan)));

                final AjaxLink<LoanBook> link = new AjaxLink<LoanBook>("refunButton") {

                    private static final long serialVersionUID = 1738204206812807611L;

                    @Override
                    @Transactional
                    public void onClick(final AjaxRequestTarget target) {
                        showModal(target);
                        final LoanEditComponent thiss = LoanEditComponent.this;
                        thiss.getLibraryServiceFacade().refun(thiss.getLibraryServiceFacade().get(loan));
                        thiss.updateTable(target);
                    }

                };

                item.add(link);
                LoanEditComponent.this.getCommentLink(item, item.getModelObject());
            }
        });
    }

    private void updateTable(final AjaxRequestTarget target) {
        final Page page = getPage();
        replaceWith(new LoanEditComponent());
        target.add(page);
    }

    private String getBookTitle(final LoanBook loanBook) {
        return getLibraryServiceFacade().titleFor(loanBook.getCopy());
    }

    private String getRefunDate(final LoanBook loan) {
        return getLibraryServiceFacade().expirationDate(loan);
    }

    private List<LoanBook> getLoans() {
        final User user = ((WebSession) getSession()).getUser();
        return getLibraryServiceFacade().allLoanForUser(user);
    }

    public static Book getBook() {
        return LoanEditComponent.book;
    }

    public static void setBook(final Book book) {
        LoanEditComponent.book = book;
    }

    public static LibraryServiceFacade getLibraryServiceFacadeLEC() {
        return LoanEditComponent.libraryServiceFacadeLEC;
    }

    public static void setLibraryServiceFacadeLEC(final LibraryServiceFacade libraryServiceFacadeLEC) {
        LoanEditComponent.libraryServiceFacadeLEC = libraryServiceFacadeLEC;
    }
}
